package com.ruoyi.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.business.domain.dto.admin.product.TbProductExportDTO;
import com.ruoyi.business.domain.product.TbProduct;
import com.ruoyi.business.domain.product.TbProductCategories;
import com.ruoyi.business.domain.product.TbProductSkus;
import com.ruoyi.business.domain.vo.product.TbProductVO;
import com.ruoyi.business.service.product.TbProductCategoriesService;
import com.ruoyi.business.service.product.TbProductService;
import com.ruoyi.business.service.product.TbProductSkusService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品主表Controller
 *
 * @author ruoyi
 * @date 2025-10-29
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/product")
@Api(tags = "商品管理接口")
public class TbProductController extends BaseController {

    private final TbProductService tbProductService;
    private final TbProductSkusService tbProductSkusService;
    private final TbProductCategoriesService tbProductCategoriesService;

    /**
     * 查询商品主表列表
     */
//    @SaCheckPermission("business:product:list")
    @GetMapping("/list")
    @ApiOperation("查询商品列表")
    public TableDataInfo<TbProductVO> list(TbProduct tbProduct, PageQuery pageQuery) {
        return tbProductService.selectPageList(tbProduct,pageQuery);
    }

    /**
     * 获取商品主表详细信息
     */
//    @SaCheckPermission("business:product:query")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取商品详情")
    public R<TbProductVO> getInfo(@ApiParam("商品ID") @PathVariable("id") Long id) {
        TbProduct byId = tbProductService.getById(id);
        if (byId == null) {
            return R.fail("商品不存在");
        }
        TbProductVO tbProductVO = new TbProductVO();
        BeanUtils.copyProperties(byId, tbProductVO);
        if (byId.getCategoryId()!=null){
            TbProductCategories byId1 = tbProductCategoriesService.getById(byId.getCategoryId());
            tbProductVO.setCategoryName(byId1.getName());
        }
        List<TbProductSkus> list = tbProductSkusService.list(new LambdaQueryWrapper<>(TbProductSkus.class)
            .eq( TbProductSkus::getProductId, id));
        tbProductVO.setSkusList(list);
        return R.ok(tbProductVO);
    }

    /**
     * 新增商品主表
     */
//    @SaCheckPermission("business:product:add")
    @PostMapping
    @ApiOperation("新增商品")
    public R<Boolean> add(@RequestBody TbProduct tbProduct) {
        //获取当前用户
        LambdaQueryWrapper<TbProduct> eq = new LambdaQueryWrapper<TbProduct>().eq(TbProduct::getSpuCode, tbProduct.getSpuCode()).eq(TbProduct::getIsDelete, 0);
        if (tbProductService.count(eq) > 0) {
            return R.fail("商品编码已存在");
        }
//        String username = Optional.ofNullable(getUsername()).orElse("系统");
//        tbProduct.setCreateBy(username);
        return R.ok(tbProductService.save(tbProduct));
    }

    /**
     * 修改商品主表
     */
//    @SaCheckPermission("business:product:edit")
    @PutMapping
    @ApiOperation("修改商品")
    public R<Boolean> edit(@RequestBody TbProduct tbProduct) {
        return R.ok(tbProductService.updateById(tbProduct));
    }

    /**
     * 删除商品主表
     */
//    @SaCheckPermission("business:product:remove")
    @DeleteMapping("/{ids}")
    @ApiOperation("删除商品")
    public R<Boolean> remove(@ApiParam("商品IDs") @PathVariable Long[] ids) {
        return R.ok(tbProductService.deleteByIds(Arrays.asList(ids)));
    }
    /**
     * 导出商品主表列表
     */
//    @SaCheckPermission("business:product:export")
    @GetMapping("/export")
    @ApiOperation("导出商品列表")
    @ResponseBody
    public void export(TbProduct tbProduct, HttpServletResponse response) {
        List<TbProduct> list = tbProductService.selectlist(tbProduct);
        // 转换为导出DTO
        List<TbProductExportDTO> exportList = list.stream()
            .map(TbProductExportDTO::convertFrom)
            .collect(Collectors.toList());

        ExcelUtil.exportExcel(exportList, "商品主表", TbProductExportDTO.class, response);
    }
}
